//
//  SAShopWKWebVC2.m
//  saasRetail
//
//  Created by mac on 2022/9/16.
//

#import "SAShopWKWebVC2.h"
#import "HDWKWebView.h"
#import <WechatOpenSDK/WXApi.h>

@interface SAShopWKWebVC2 ()

@end

@implementation SAShopWKWebVC2

- (void)viewDidLoad {
    [super viewDidLoad];
    
}

-(instancetype)initWithTitle:(NSString *)title AndUrl:(NSString *)url {
    if (self =  [super init]) {
        HDWKWebView *webView = [[HDWKWebView alloc] initWithConfig:@[@"verifyError",@"wxShare"] frame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
        webView.url = url;
        webView.isAdaptive = YES;
        [webView startLoadUrl];
        WeakSelf
        webView.jsActionBlock = ^(NSString *name, id body) {
            if ([name isEqualToString:@"verifyError"]) {
                [weakSelf.navigationController popViewControllerAnimated:YES];
            }else if ([name isEqualToString:@"wxShare"]) {
                NSData *jsonData = [body dataUsingEncoding:NSUTF8StringEncoding];
                NSError *err;
                NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData
                                                                     options:NSJSONReadingMutableContainers
                                                                       error:&err];
                
                if ([WXApi isWXAppInstalled] && [WXApi isWXAppSupportApi]) {
                    NSString *titleStr = dic[@"title"];
                    if (titleStr.length == 0) {
                        titleStr = @"衣点通零售";
                    }
                    NSString *imageUrlStr = dic[@"imageUrl"];
                    if (imageUrlStr.length == 0) {
                        imageUrlStr = @"";
                    }
                    NSString *pathStr = dic[@"path"];
                    if (pathStr.length == 0) {
                        pathStr = @"";
                    }
                    
//                    SendMessageToWXReq *req = [[SendMessageToWXReq alloc] init];
//                    req.scene = WXSceneSession;
//                    WXMediaMessage *message = [WXMediaMessage message];
//                    message.title = titleStr;
//                    message.description = @"";
//
//                    [message setThumbData:[NSData dataWithContentsOfURL:[NSURL URLWithString:imageUrlStr]]];
//
//                    WXWebpageObject *webObj = [WXWebpageObject object];
//                    webObj.webpageUrl = pathStr;
//                    message.mediaObject = webObj;
//                    req.message = message;
//                    [WXApi sendReq:req completion:^(BOOL success) {
//                        NSLog(@"打开微信小程序:%@", success ? @"成功" : @"失败");
//                    }];
                    

                    WXMiniProgramObject *wxMiniObject = [WXMiniProgramObject object];
                    wxMiniObject.userName = @"gh_146cb32ec7ad";
                    wxMiniObject.webpageUrl = @""; //打不开小程序的自动转成网页
                    wxMiniObject.path = pathStr;
//                    wxMiniObject.hdImageData = image;
                    
                    WXMediaMessage *message = [WXMediaMessage message];
                    message.title = titleStr;
                    message.description = @"";
                    message.mediaObject = wxMiniObject;
                    [message setThumbData:[NSData dataWithContentsOfURL:[NSURL URLWithString:imageUrlStr]]];
                    
                    SendMessageToWXReq *req = [[SendMessageToWXReq alloc] init];
                    req.message = message;
                    req.scene = WXSceneSession;
                    [WXApi sendReq:req completion:^(BOOL success) {
                        NSLog(@"打开微信小程序:%@", success ? @"成功" : @"失败");
                    }];
                }
            }
        };
        [self.view addSubview:webView];
        
        
    }
    return self;
}

#pragma mark - initUI
-(void)initUI {
    
}

@end
